package system

import (
	"k8s-admin/models"
	"k8s-admin/service/system"
	"k8s-admin/utils"

	"github.com/gin-gonic/gin"
)

func CreateService(ctx *gin.Context) {
	req := models.ServiceReq{}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		err := system.NewServiceInfo().CreateService(req.UserName, req.Password, req.AuthKey, req.IPAddress,
			req.ServiceName, req.ServiceVersion, req.Status, req.Port)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err)
		} else {
			utils.ReturnContext(ctx).Successful("成功", "创建成功")
		}
	}
}

func List(ctx *gin.Context) {
	list, err := system.NewServiceInfo().List()
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {

		utils.ReturnContext(ctx).Successful("成功", list)
	}
}

func Delete(ctx *gin.Context) {
	param := new(struct {
		PropertyNumber string `json:"property_number" form:"property_number" binding:"required"`
	})
	if err := ctx.ShouldBind(&param); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		err = system.NewServiceInfo().Delete(param.PropertyNumber)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "删除成功")
		}
	}
}

func Update(ctx *gin.Context) {
	params := new(struct {
		ServiceVersion string `json:"service_version" binding:"required"`
		PropertyNumber string `json:"property_number" binding:"required"`
		UserName       string `json:"user_name" binding:"required"`
		Password       string `json:"password" binding:"required"`
		Port           int    `json:"port" binding:"required"`
		IPAddress      string `json:"ip_address" binding:"required"`
		ServiceName    string `json:"service_name" binding:"required"`
		AuthKey        string `json:"auth_key"`
		Status         *int   `json:"status"`
	})
	if err := ctx.ShouldBindJSON(&params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		err := system.NewServiceInfo().UpdateService(params.PropertyNumber, params.UserName, params.Password, params.AuthKey, params.IPAddress,
			params.ServiceName, params.ServiceVersion, *params.Status, params.Port)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err)
		} else {
			utils.ReturnContext(ctx).Successful("成功", "更新成功")
		}
	}
}
